package com.auto.learning.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.bean.ImageCommentBean;
import com.auto.learning.event.Handle;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.view.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 评论fragment Adapter
 * @author wangjianjun
 */
public class CommentFragmentAdapter extends BaseAdapter {
	
	private Context context;
	private Handle handle;
	private ArrayList<ImageCommentBean> list;
	DisplayImageOptions options;
	
	public CommentFragmentAdapter(Context context,ArrayList<ImageCommentBean> list,Handle handle){
		this.context = context;
		this.list = list;
		this.handle = handle;
		initImageLoader();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return list.get(position).getId();
	}

	ViewHolder viewHolder;
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ImageCommentBean bean = list.get(position);
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.comment_fragment_adapter, null);
			viewHolder.gridView = (GridView) convertView.findViewById(R.id.gv_image_list);
			viewHolder.tv_publish_time = (TextView) convertView.findViewById(R.id.tv_publish_time);
			viewHolder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
			viewHolder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
			viewHolder.fg_me_userpic = (CircleImageView) convertView.findViewById(R.id.fg_me_userpic);
			viewHolder.tv_favour_num = (TextView) convertView.findViewById(R.id.tv_favour_num);
			viewHolder.tv_comment_delete = (TextView) convertView.findViewById(R.id.tv_comment_delete);
			viewHolder.tv_comment_num = (TextView) convertView.findViewById(R.id.tv_comment_num);
			viewHolder.rel_favour = (RelativeLayout) convertView.findViewById(R.id.rel_favour);
			viewHolder.rel_comment = (RelativeLayout) convertView.findViewById(R.id.rel_comment);
			viewHolder.image_fav = (ImageView) convertView.findViewById(R.id.image_fav);
			viewHolder.tv_comment_share = (TextView) convertView.findViewById(R.id.tv_comment_share);
			viewHolder.image_report = (ImageView) convertView.findViewById(R.id.image_report);
			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (bean.getIsSelf() == 2) {
			viewHolder.image_report.setVisibility(View.INVISIBLE);
		}else {
			viewHolder.image_report.setVisibility(View.VISIBLE);
		}
		int columnNumbers = 0;
		if (bean.getImgList() != null && bean.getImgList().size() > 0) {
			switch (bean.getImgList().size()) {
			case 1:
				columnNumbers = 1;
				break;
			case 2:
			case 4:
				columnNumbers = 2;
				break;
			default:
				columnNumbers = 3;
				break;
			}
		}
		if (bean.getImgList() != null && bean.getImgList().size() == 0) {
			viewHolder.gridView.setVisibility(View.GONE);
		}else {
			viewHolder.gridView.setVisibility(View.VISIBLE);
			viewHolder.gridView.setNumColumns(columnNumbers);
			LayoutParams layoutParams = (LayoutParams) viewHolder.gridView.getLayoutParams();
//			if (columnNumbers == 1) {
//				layoutParams.width = CommonMethod.dip2px(context, 130) * columnNumbers;
//				layoutParams.height = CommonMethod.dip2px(context, 180) * columnNumbers;
//			}else {
			//所有评论的图片统一大小
				layoutParams.width = CommonMethod.dip2px(context, 100) * columnNumbers;
				layoutParams.height = CommonMethod.dip2px(context, 100) * columnNumbers;
				//			}
			viewHolder.gridView.setLayoutParams(layoutParams);
			CommentImageAdapter commentImageAdapter = new CommentImageAdapter(context,bean.getImgList(),handle);
			viewHolder.gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
			viewHolder.gridView.setAdapter(commentImageAdapter);
			if (bean.getImgList().size() > 1) {
				CommonMethod.setGridViewHeight(viewHolder.gridView, columnNumbers, CommonMethod.dip2px(context, 3));
			}
		}
		ImageLoader.getInstance().displayImage(bean.getFace(), viewHolder.fg_me_userpic,options);
		viewHolder.tv_content.setText(bean.getComment()+"");
		viewHolder.tv_publish_time.setText(bean.getCreateTime()+"");
		viewHolder.tv_username.setText(bean.getName()+"");
		viewHolder.tv_favour_num.setText(bean.getFavourNum()+"");
		viewHolder.tv_comment_num.setText(bean.getReplyNum()+"");
		if (bean.getIsSelf() == 2) {
			viewHolder.tv_comment_delete.setVisibility(View.VISIBLE);
		}else {
			viewHolder.tv_comment_delete.setVisibility(View.GONE);
		}
		if (bean.getIsFavour() == 2) {
			viewHolder.image_fav.setImageResource(R.drawable.pinlun_zan_green);
			viewHolder.tv_favour_num.setTextColor(context.getResources().getColor(R.color.main_green_color));
		}else {
			viewHolder.image_fav.setImageResource(R.drawable.pinlun_zan_gray);
			viewHolder.tv_favour_num.setTextColor(context.getResources().getColor(R.color.text_comment_little_color));
		}
		viewHolder.tv_comment_delete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				handle.obtainMessage(9, bean.getBookId(), bean.getId()).sendToTarget();
			}
		});
		viewHolder.rel_favour.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("commentId", bean.getId());
				map.put("bookId", bean.getBookId());
				map.put("isFavour", bean.getIsFavour());
				handle.obtainMessage(4, map).sendToTarget();
			}
		});
		viewHolder.tv_comment_share.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				handle.obtainMessage(12, bean.getId(), position).sendToTarget();
			}
		});
		viewHolder.rel_comment.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				handle.obtainMessage(11, bean.getId(), position).sendToTarget();
			}
		});
		viewHolder.image_report.setOnClickListener(new OnClickListener() {  //举报
			
			@Override
			public void onClick(View v) {
				handle.obtainMessage(13, bean.getId(), position).sendToTarget();
			}
		});
		/*viewHolder.image_oper.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);  
				imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
				int[] location = new int[2];
				view.getLocationOnScreen(location);
				location[0] -= 525;
				location[1] -= 30;
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("commentId", bean.getId());
				map.put("bookId", bean.getBookId());
				map.put("isFavour", bean.getIsFavour());
				map.put("replyUser", bean.getUserId()); // 被回复者用户ID
				MyCommentFragmentPopWindowDialog dialog = new MyCommentFragmentPopWindowDialog(context, map, location, view);
				dialog.setOnClick(new IPopDialogOnclickInterface() {

					@Override
					public void OnSupportClick(Map<String, Object> map) {
						handle.obtainMessage(4, map).sendToTarget();
					}

					@Override
					public void OnRepClick(Map<String, Object> map) {
						handle.obtainMessage(1, position, 0, map).sendToTarget();
					}
				});
				dialog.showDialog();
			}
		});*/
		return convertView;
	}
	
	class ViewHolder{
		GridView gridView;
		TextView tv_publish_time,tv_username,tv_content,tv_favour_num,tv_comment_delete,tv_comment_num,tv_comment_share;
		CircleImageView fg_me_userpic;
		RelativeLayout rel_comment,rel_favour;
		ImageView image_fav,image_report;
	}
	
	private void initImageLoader(){
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.me_user)
		.showImageForEmptyUri(R.drawable.me_user)
		.showImageOnFail(R.drawable.me_user).cacheInMemory(true)
		.cacheOnDisk(true).considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();
	}
}
